﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using xna = Microsoft.Xna.Framework;
using URWPGSim2D.Common;
using URWPGSim2D.StrategyLoader;

namespace URWPGSim2D.Strategy
{
    public class Strategy : MarshalByRefObject, IStrategy
    {
        #region reserved code never be changed or removed
        /// <summary>
        /// override the InitializeLifetimeService to return null instead of a valid ILease implementation
        /// to ensure this type of remote object never dies
        /// </summary>
        /// <returns>null</returns>
        public override object InitializeLifetimeService()
        {
            //return base.InitializeLifetimeService();
            return null; // makes the object live indefinitely
        }
        #endregion

        /// <summary>
        /// 决策类当前对象对应的仿真使命参与队伍的决策数组引用 第一次调用GetDecision时分配空间
        /// </summary>
        private Decision[] decisions = null;

        /// <summary>
        /// 获取队伍名称 在此处设置参赛队伍的名称
        /// </summary>
        /// <returns>队伍名称字符串</returns>
        public string GetTeamName()
        {
            return "水中搬运 Test Team";
        }
        public  int  Flag =0;
        public int   Flash = 0;
        /// <summary>
        /// 获取当前仿真使命（比赛项目）当前队伍所有仿真机器鱼的决策数据构成的数组
        /// </summary>
        /// <param name="mission">服务端当前运行着的仿真使命Mission对象</param>
        /// <param name="teamId">当前队伍在服务端运行着的仿真使命中所处的编号 
        /// 用于作为索引访问Mission对象的TeamsRef队伍列表中代表当前队伍的元素</param>
        /// <returns>当前队伍所有仿真机器鱼的决策数据构成的Decision数组对象</returns>
        public Decision[] GetDecision(Mission mission, int teamId)
        {
            // 决策类当前对象第一次调用GetDecision时Decision数组引用为null
            if (decisions == null)
            {// 根据决策类当前对象对应的仿真使命参与队伍仿真机器鱼的数量分配决策数组空间
                decisions = new Decision[mission.CommonPara.FishCntPerTeam];
            }


            int b0 = Convert.ToInt32(mission.HtMissionVariables["Ball0InHole"]);
            int b1 = Convert.ToInt32(mission.HtMissionVariables["Ball1InHole"]);
            int b2 = Convert.ToInt32(mission.HtMissionVariables["Ball2InHole"]);
            int b3 = Convert.ToInt32(mission.HtMissionVariables["Ball3InHole"]);
            int b4 = Convert.ToInt32(mission.HtMissionVariables["Ball4InHole"]);
            int b5 = Convert.ToInt32(mission.HtMissionVariables["Ball5InHole"]);




            //判断是否目标区域是否有球
            int panduansanmen = FishDecision.FishDecision.Sanmen(mission.EnvRef.Balls[3].PositionMm) ;
            int panduanwumen = FishDecision.FishDecision.Wumen(mission.EnvRef.Balls[5].PositionMm);
            int panduansimen = FishDecision.FishDecision.Simen(mission.EnvRef.Balls[0].PositionMm)  ;
            int panduanyimen = FishDecision.FishDecision.Yimen(mission.EnvRef.Balls[1].PositionMm)  ;
            int panduanermen = FishDecision.FishDecision.Ermen(mission.EnvRef.Balls[2].PositionMm)  ;
            int panduanlingmen = FishDecision.FishDecision.Lingmen(mission.EnvRef.Balls[4].PositionMm);

            //1鱼游动
            if (Flag == 0)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(1160, 0, -530), 45);
                if(FishDecision.FishDecision.JudgeArea1(mission.TeamsRef[0].Fishes[1].PositionMm) == 1)
                {
                   Flag = 1;
                }
            }
            if (Flag == 1)
            {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[1], 0, 1, 3, new xna.Vector3(-475, 0, -320),5 , 5 ,5 ,6 ,3 , 1);
                //获取临时坐标点                                      
              //  xna.Vector3 potision3 = FishDecision.FishDecision.SetDestPtMm(new xna.Vector3(-475, 0, -320), mission.EnvRef.Balls[3].PositionMm, 0, 35);


                //获得目标方向                                   
               // float mubiaofangxiang3 = FishDecision.FishDecision.GetDirRad(mission.TeamsRef[0].Fishes[1].PositionMm, new xna.Vector3(-475, 0, -320));


                //带球算法Dribble算法

               // FishDecision.FishDecision.Dribble(ref decisions[1], mission.TeamsRef[0].Fishes[1], potision3, mubiaofangxiang3, (float)Math.PI / 2, (float)Math.PI / 4, 300, 14, 14, 100, 10, true);
                if (b3==1)
                {
                    Flag = 2;
                }
            } 
           //1鱼继续游动
           if(Flag==2)
            {
                 FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(1015, 0,-380), 90);
                 if (FishDecision.FishDecision.JudgeArea11(mission.TeamsRef[0].Fishes[1].PositionMm) == 1)
                 {
                     Flag = 3;
                 }
            }
            if(Flag==3)
            {
                 FishDecision.FishDecision.Dribble_Will(mission, ref decisions[1], 0, 1, 1, new xna.Vector3(-240, 0, -160), 5, 5, 5, 3,3,2);
                 if (b1== 1)
                 {
                    Flag = 4;
                 }
            } 
            //1鱼再游动
            if(Flag==4)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(800, 0, -200), 135);
                if ((FishDecision.FishDecision.JudgeArea111(mission.TeamsRef[0].Fishes[1].PositionMm)==1))
                {
                    Flag = 5;
                }
            }
            if(Flag==5)
            {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[1], 0, 1, 0, new xna.Vector3(0, 0, 0), 5, 5, 5, 6, 3, 1);
                if (b0== 1)
                {
                    Flag = 6;
                }
            }
            if(Flag==6)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[1], 0, 1, new xna.Vector3(-1160, 0, -530), 45);
            }









            //0鱼游动
           if (Flash == 0)
            {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0],0, 0, new xna.Vector3(1160,0,530), -45);
                if (FishDecision.FishDecision.JudgeArea0(mission.TeamsRef[0].Fishes[0].PositionMm) == 1)
                {
                     Flash = 1;
                }
            }
            if (Flash == 1)
            {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0],0,0,5, new xna.Vector3(-475, 0, 320), 5, 5, 5, 6, 3, 1);
                if (b5== 1)
                {
                    Flash=2;
                }
            }
            
            //0鱼继续游动
            if (Flash == 2)
             {
                 FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(1015, 0,380), -90);
                 if (FishDecision.FishDecision.JudgeArea00(mission.TeamsRef[0].Fishes[0].PositionMm) == 1)
                 {
                    Flash = 3;
                 }
             }
             if (Flash == 3)
             {
                 FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0, 0, 2 , new xna.Vector3(-240, 0, 160), 5, 5, 5, 6,3,1);
                 if(b2==1)
                {
                    Flash = 4;
                }
             }

             //0鱼再游动
             if(Flash==4)
             {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(1080,0,272), -135);
                if (FishDecision.FishDecision.JudgeArea000(mission.TeamsRef[0].Fishes[0].PositionMm) == 1)
                {
                    Flash = 5;
                }
             }
             if(Flash==5)
             {
                FishDecision.FishDecision.Dribble_Will(mission, ref decisions[0], 0, 0, 4, new xna.Vector3(-480, 0, 0), 5, 5, 5, 6, 3, 1);
                if(b4==1)
                {
                    Flash = 6;
                }
             }
             if(Flash==6)
             {
                FishDecision.FishDecision.MoveToAreaS(mission, ref decisions[0], 0, 0, new xna.Vector3(-1160, 0, 530), -45);
             }







            return decisions;
        }
    }
}
